/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package justdoit.utility;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import org.jdom.Document;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

/**
 * Only offer functions deal with XML file and elements, know nothing about
 * Task and the structure of XML
 * @author Can
 */
public class XMLUtility {

    // write the document to a file
    public static int writeXMLFile(Document doc, String fileName) {
        try {
            FileOutputStream output = new FileOutputStream(fileName);
            XMLOutputter outputter = new XMLOutputter();
            outputter.setFormat(Format.getPrettyFormat());
            outputter.output(doc, output);
            return 0;

        } catch (IOException e) {
            System.out.println(e.getMessage());
            return -1;
        }
    }

    // return -1 if error, 0 if success
    public static int createXMLFile(Document document, String fileName) {
        File f = new File(fileName);
        try {
            f.createNewFile();
        } catch (IOException e) {
            System.out.println(e.getMessage());
            return -1;
        }
        return XMLUtility.writeXMLFile(document, fileName);
    }

    public static void main(String[] args) throws IOException {
    }
}
